import { defineStore } from 'pinia'
import { createCommonGetters, createCommonActions } from "../helper"
import { getParamsRequiredKeys } from "@/store"
import $api from "@/api"
export const useCartSummaryStore = defineStore('cart-summary', {
    state: () => ({
        loading: false,
        params: {
            snapshotId: "",
            subject: "语文",
            grade: 10,
        },
        data: {
            list: [],
            total: 0,
            ids: [],
            snapshotId: ""
        },

    }),
    getters: {
        ...createCommonGetters
    },
    actions: {
        ...createCommonActions(),
        async fetchCartSummary(params = {}) {
            this.loading = true
            try {
                let newParams = { ...this.params, ...params }
                let subParams = getParamsRequiredKeys(newParams, ["snapshotId"])
                const res = await $api.paper.cartSummary({ ...subParams })
                if (res.code === 200) {
                    this.data.list = res.data?.details || []
                    this.data.total = res.data?.count || 0
                
                    this.data.snapshotId = res.data?.snapshotId || ""
                }
                else {
                    $modal.message({ message: '【$api.paper.cartSummary', type: 'error' })
                }

            }
            catch (error) {
                console.log("fetchCartSummary", error)
            }
        }

    },
    persist: {
        enabled: true,
        storage: localStorage,
    }
})
